// Copyright Shane D. Pinder, 1998-2011, All Rights Reserved

#include <gpdr.h>
#include <delay.h>
#include <stdio.h>
#include <lcd.h>
#include <lcd_control.h>
#include <sim.h>

char fdata[13] = "startlog.rtf";
volatile unsigned char converting = 1;
unsigned long int sample_period = 3600;
unsigned char sample_table = 0;
unsigned int sample_period_table[1440];
long int ymdh0;
extern volatile unsigned char vini_buffer_full;
extern volatile unsigned char full_messages;
extern volatile unsigned char lcd_present;
extern volatile unsigned char gps_present;
extern volatile unsigned char sim_present;

void init_USB()
{
strf2buffer("\r\rInitialisation\r");

lithium_recharge(168);

delay_ms(4000); // Red-Green-Red-Green-Red-Off

wait4message(); // On-Line
//delay_ms(500); // Green-Red Steady

wait4message(); // DD P2
//delay_ms(500); // Flashing Red - (Drive Flashes a few times, then off)

wait4message(); // No Upgrade
//delay_ms(500); // Red Steady

wait4message(); // Prompt
//delay_ms(500); // Red Steady

check4time_file();
//delay_ms(500); // One Flash Red, then Steady - (Drive Flashes a few times, then off) 

check4setup_file();
//delay_ms(500); // One Flash Red, then Steady - (Drive Flashes a few times, then off)

open_file4write(fdata, 1);
//delay_ms(500); // One Flash Red, then Steady - (Drive Flashes a few times, then off)

write_buffer2file(0);
//delay_ms(500); // One Flash Red, then Steady - (Drive Flashes a few times, then off)

close_file_nf(fdata);
//delay_ms(500); // One Flash Red, then Steady - (Drive Flashes a few times, then off)
 
//suspend_monitor(); // First iteration of wait_sample_period() does not wake monitor
}

void init_LCD()
{
if (lcd_present == 1)
    {
    lcd_init(16);

    // Display the cursor and blinking
    _lcd_ready();
    _lcd_write_data(0x0f);

    // Write message
    lcd_display_status("Init");
    }
}

void init_GPS()
{
if (gps_present == 1)
    {
    DDRD=0x0A; // for UART
    init_usarts();
    delay_ms(1000);

    gps_putstrf("$PMTK220,1000*1F\r\n"); // set GPS message rate 1 Hz
    delay_ms(1000);

    gps_putstrf("$PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*28\r\n"); // set GPGGA & GPRMC output
    delay_ms(1000);
    }
    
    delay_ms(1000);
}

void init_SIM()
{   
if (sim_present == 1)
    {
    DDRD=0x0A; // for UART
    init_usarts();
    DDRD.7 = 1; // Boot SIM
    delay_ms(2000);
    DDRD.7 = 0;
    delay_ms(5000);
    
    
    delete_all_messages();
   
    
    sim_putstrf("A");
    delay_ms(2000);
    sim_putstrf("T\r\n");
    delay_ms(2000);
    sim_putstrf("AT\r\n");
    delay_ms(2000);
    sim_putstrf("AT\r\n"); 
    delay_ms(2000);
    
   
    
    }
}

void wait_sample_period()
{
unsigned char rtc_msg[7];
unsigned char ymdhms[6];
unsigned long int seconds, i;
char msg[50];

open_file4write(fdata, 0);

write_buffer2file(0);

close_file_nf(fdata);

vini_buffer_full = 0;

suspend_monitor();

rtc_read(rtc_msg);
rtc2ymdhms(rtc_msg, ymdhms);
seconds = (unsigned long int)(ymdhms[3])*3600 + (unsigned long int)(ymdhms[4])*60 + (unsigned long int)(ymdhms[5]);

ADCSRA = 0x8B; // Enable ADC
ADCSRA|= 0x40;

if (seconds % sample_period < sample_period*9/10)
    {
    for (i=0;i<(sample_period - seconds % sample_period - 1);i++)
        {
        while (converting < 1) // wait for first sample to complete
            {
            }      

        clock(); // delete once hardware INT is connected

        while (converting > 0) // wait for last sample to complete
            {
            }      
        }
    }

ADCSRA = 0x0B; // Disable ADC

delay_ms(20);

wake_monitor();

do
    {
    delay_ms(49);
        
    if ((spi_status()&0x02) == 0)
        {
        wait4prompt();
        }

    rtc_read(rtc_msg);
    rtc2ymdhms(rtc_msg, ymdhms);
    seconds = (unsigned long int)(ymdhms[3])*3600 + (unsigned long int)(ymdhms[4])*60 + (unsigned long int)(ymdhms[5]);
    } while (seconds % sample_period > 0);

if (full_messages == 1)
    {
    sprintf(msg, "Period: %lu Time: %lu %i %02i %02i %02i:%02i:%02i Remainder: %lu\r", sample_period, seconds, ymdhms[0], ymdhms[1], ymdhms[2], ymdhms[3], ymdhms[4], ymdhms[5], (seconds % sample_period));
    str2buffer(msg);
    }

rtc_read(rtc_msg);
rtc2ymdhms(rtc_msg, ymdhms);

sprintf(fdata, "%02i%02i%02i%02i.rtf", ymdhms[0], ymdhms[1], ymdhms[2], ymdhms[3]);

//char2buffer(ymdhms[0]); 
//char2buffer(ymdhms[1]); 
//char2buffer(ymdhms[2]); 
//char2buffer(ymdhms[3]); 
char2buffer(ymdhms[4]); 
char2buffer(ymdhms[5]);
strf2buffer("\r");

if (sample_table == 1)
    {
    sample_period = (unsigned long int)sample_period_table[(unsigned long int)ymdhms[0]*12*31*24 + (unsigned long int)ymdhms[1]*31*24 + (unsigned long int)ymdhms[2]*24 + (unsigned long int)ymdhms[3] - ymdh0];

    if ((sample_period < 5) || (sample_period > 3600))
        {
        sample_period = 60;
        }
    }
}

void sample_adc()
{
//DDRC.3 = 1; // Dust LED On
//delay_us(250);

ADCSRA|= 0x40;
converting = 1;
     
//while (converting > 0)  
//    {                
//    }  

//DDRC.3 = 0; // Dust LED Off

//comparator_stopwatch0(0); // measure humidity

//comparator_stopwatch1(1); // measure temperature
}
